import { TimeManager } from './../timeManager';
import { Pipe, PipeTransform } from '@angular/core';
import { Observable } from 'rxjs';

@Pipe({ name: 'delayTimer' })
export class DelayTimer implements PipeTransform {
    transform(delay: number): Observable<number> {
        return Observable.interval(delay);
    }
}

//todo
@Pipe({ name: 'elapsedTime' })
export class ElapsedTime implements PipeTransform {
    transform(count: number, fromMilliseconds: number): number {
        return TimeManager.current.getServerTime() - fromMilliseconds;
    }
}

@Pipe({ name: 'msecToS' })
export class MsecToS implements PipeTransform {
    transform(milliseconds: number): number {
        return Math.floor(milliseconds / 1000)
    }
}

@Pipe({ name: 'orderBy' })
export class OrderBy implements PipeTransform {
    transform(array: Array<any>, args: string): Array<any> {
        array.sort((x, y) => {
            return (y[args] || Number.MAX_VALUE) - (x[args] || Number.MAX_VALUE);
        })
        return array;
    }
}

@Pipe({ name: 'mapToIterable' })
export class MapToIterable implements PipeTransform {
    transform(obj: Object): Array<{
        key: string;
        value: any
    }> {
        if (!obj) return [];
        const array = Object.keys(obj).map((key) => {
            return {
                key,
                value: obj[key]
            }
        });
        return array;
    }
}